import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class Processing_image extends PApplet {

int savedTime;
int savedTimebis;
int savedTimeter;
int totalTime = 200;
int totalTimebis = 1;
int totalTimeter = 1000;
int score;

float spwn;
int mort = 0;
Vaisseau v1 = new Vaisseau(50, 50);  
ArrayList<EnemyV> EnemyVe = new ArrayList<EnemyV>();
ArrayList<Missile> Missiles = new ArrayList<Missile>();
ArrayList<MissileE> MissilesE = new ArrayList<MissileE>();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void setup() { 
  


  background(0);
  size(1024, 768);
  mort = 0;
  score = 0;
  savedTime = millis();

  for (int v =0; v<5; v++) {
    EnemyVe.add(new EnemyV());

    for (EnemyV enemy : EnemyVe) {
      enemy.draw();
    }
  }
  v1.draw();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void draw() {

  text("Score:"+score, 10, 768-10);
  if (mort == 0) {
    int passedTime = millis() - savedTime;


    if (passedTime> totalTime) {
      background(0);
      for (EnemyV enemy : EnemyVe) {
        enemy.update();
      }
      v1.noupdate();

      savedTime = millis();
    }

    int passedTimebis = millis() - savedTimebis;
    if (passedTimebis> totalTimebis) {
      for (Missile missi : Missiles) {
        missi.update();
      }
      for ( MissileE missie : MissilesE) {
        missie.update();
      }
      savedTimebis = millis();
    }
    int passedTimeter = millis() - savedTimeter;
    if (passedTimeter> totalTimeter) {
      for (int j=0; j< EnemyVe.size (); j++) {
        EnemyV enem = EnemyVe.get(j);
        spwn = random(100);
        int posxene = enem.xpos;
        int posyene = enem.ypos;
        if (spwn<10) {
          MissilesE.add(new MissileE(posyene+10, posyene+10, posxene+5));
          for (int n = 0; n < MissilesE.size (); n++) {
            MissileE misse = MissilesE.get(n);
            misse.spawn();
          }
        }
        savedTimeter = millis();
      }
    }

    for ( int m=0; m<EnemyVe.size (); m++) {
      EnemyV enem = EnemyVe.get(m);
      int posxene = enem.xpos;
      int posyene = enem.ypos;
      for (int i = 0; i < Missiles.size (); i++) {  
        Missile miss = Missiles.get(i);
        int posxmis = miss.posx;
        int posymis = miss.posmi;    
        if (posxene-5<=posxmis && posxmis<=posxene+50) {    
          if (posyene+10>=posymis && posymis>=posyene+5) {
            EnemyVe.remove(m);
            Missiles.remove(i);
            score = score+10;
          }
        }
      }
    }
    for (int k=0; k< MissilesE.size (); k++) {
      MissileE misse = MissilesE.get(k);
      int posxmise = misse.posx;
      int posymise = misse.posmi;
      if (v1.xposv-30<=posxmise && posxmise<=v1.xposv+89) {    
        if (v1.yposv+50>=posymise && posymise>=v1.yposv+19) {

          MissilesE.remove(k);
          mort = 1;
        }
      } 
      if (posymise>displayHeight-20) {
        MissilesE.remove(k);
      }
    }

    if (EnemyVe.size()==0 || mort == 1 ) {
      textAlign(CENTER, BOTTOM);
      text("GAME OVER", 1024/2, 768/2);
      text("Press R to restart", 1024/2, 768/2+50);   
      text("Press Esc to exit", 1024/2, 768/2+100);
    }
  }
  if (keyCode == ESC) {
    exit();
  }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void keyPressed() {
  if (EnemyVe.size()!=0 && mort == 0) {
    if (keyCode == RIGHT) {
      v1.updateright();
    } else if (keyCode == LEFT) {
      v1.updateleft();
    }
  }
  if (EnemyVe.size()==0 || mort == 1 ) {
    if (keyCode == 'R') {      
      for (int i = MissilesE.size () - 1; i >= 0; i--) {
        MissileE miss = MissilesE.get(i);  
        MissilesE.remove(i);
      }
      for (int j = EnemyVe.size () -1; j >= 0; j --) {
        EnemyV enem = EnemyVe.get(j);
        EnemyVe.remove(j);
      }
      for (int k = Missiles.size () -1; k >= 0; k --) {
        Missile missi = Missiles.get(k);
        Missiles.remove(k);
      }

      setup();
    }
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void keyReleased() {
  if (EnemyVe.size()!=0 && mort == 0 ) {
    if (keyCode == ' ') {

      Missiles.add(new Missile());

      for (Missile miss : Missiles) {
        miss.spawn();
      }
    }
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class EnemyV {
  int xpos = 10;                                              //Space X position
  int ypos = 10;                                                //Space Y position
  int deltaX = 70;
  int deltaXd = 70;  
  PImage EnemyV ;

  public void draw() {

    EnemyV = loadImage("data/EnemyVpetit.png")   ; 
    image(EnemyV, xpos, ypos);
    xpos = xpos+deltaX;
    if (xpos>1024-50) {     
      ypos=ypos +30;       
      deltaX = -deltaX-10;
    }
    if (xpos<30) {      
      ypos=ypos+30;
      deltaX=-deltaX+10;
    }
  }
  public void update() {
    xpos = xpos+deltaX;
    if (xpos>1024-50) {
      xpos = xpos-deltaX;
      ypos=ypos +30; 
      deltaX= -deltaX;
    }
    if (xpos<30) {
      xpos = xpos-deltaX;
      ypos=ypos+30;
      deltaX=-deltaX;
    }    
    EnemyV = loadImage("data/EnemyVpetit.png")  ;  
    image(EnemyV, xpos, ypos);
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Vaisseau {
  int xposv;
  int yposv;
  int largeurv;
  int hauteurv;
  PImage Vessel;

  Vaisseau(int lv, int hv) {
    xposv = (1024-lv)/2;
    yposv = (768-hv-10);
    largeurv = lv;
    hauteurv = hv;
  }
  public void draw() {
    Vessel = loadImage("data/Vesselpetitpetit.png");

    image(Vessel, xposv, yposv);
  }
  public void updateright() {
    if (xposv>width-(largeurv+20)) {

      rect(xposv, yposv, largeurv, hauteurv);
    } else {
      xposv=xposv+25;
      Vessel = loadImage("data/Vesselpetitpetit.png");
      image(Vessel, xposv, yposv);
    }
  }
  public void updateleft() {
    if (xposv<20) {

      rect(xposv, yposv, largeurv, hauteurv);
    } else {
      xposv=xposv-25;
      Vessel = loadImage("data/Vesselpetitpetit.png");
      image(Vessel, xposv, yposv);
    }
  }
  public void noupdate() {
    Vessel = loadImage("data/Vesselpetitpetit.png");
    image(Vessel, xposv, yposv);
  }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Missile {
  PImage Missile;
  int posm;
  int posmi = (v1.yposv)-25;
  int posx = v1.xposv+v1.largeurv/2;  

  ;      // <<<<Bob-On sait pas pourquoi tu es l\u00e0, mais on t'aime bien alors tu restes ici.


  public void spawn() {
    posm = (v1.yposv)-25;                                   //hauteur de d\u00e9part du missile
    Missile = loadImage("data/tassecafepetit.png");
    image(Missile, v1.xposv+v1.largeurv/2, posm);
  }


  public void update() {


    posmi = posmi- 5;

    Missile = loadImage("data/tassecafepetit.png");
    image(Missile, posx, posmi);
  }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class MissileE {  
  int posm;
  int posmi;
  int posx;  
  PImage MissileE;
  MissileE(int vara, int varb, int varc) {
    posm = vara;
    posmi = varb;
    posx = varc;
  }
  public void spawn() {
    MissileE = loadImage("data/tassevidepetit.png");
    image(MissileE, posx, posm);
  }


  public void update() {


    posmi = posmi+ 5;
    MissileE = loadImage("data/tassevidepetit.png");
    image(MissileE, posx, posmi);
  }
}

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "Processing_image" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
